package com.glodon.ggzy.jgpt.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.glodon.ggzy.fwpt.dao.IGgZfContractDao;
import com.glodon.ggzy.jgpt.service.IJgptZfcgService;
import com.glodon.ggzy.vo.ZfcgStaticsVO;

@Service
@Transactional
public class JgptZfcgServiceImpl implements IJgptZfcgService {
	
	@Autowired
	IGgZfContractDao ggZfContractDao;

	@Override
	public List<ZfcgStaticsVO> getZfcgStaticsVO(Date begin , Date end) {
		// TODO Auto-generated method stub
		
		
		//采购人个数 
		StringBuffer cgrHql = new StringBuffer();
		cgrHql.append(" select to_char(contract.buysigntime,'yyyy') , to_char(contract.buysigntime,'mm') ,count(*)");
		cgrHql.append(" from GgZfContract contract ");
		cgrHql.append(" where contract.buysigntime between :begin and :end ");
		cgrHql.append(" and  contract.buysigntime is not null ");
		cgrHql.append(" group by    to_char(contract.buysigntime,'yyyy') , to_char(contract.buysigntime,'mm'),contract.buyerid ");
		
		Map<String,Date> parameter = new HashMap<String,Date>();
		parameter.put("begin", begin);
		parameter.put("end", end);
		List<Object> cgr = ggZfContractDao.find(cgrHql.toString(),parameter);
		Map<String,Long> cgrMap = new HashMap<String,Long>();
		for (int i = 0; i < cgr.size(); i++) {
			Object[] object = (Object[])cgr.get(i);
			if(StringUtils.isEmpty((String)object[0]))continue;
			String year = (String)object[0];
			String month = (String)object[1];
			Long count = (Long)object[2];
			cgrMap.put(year + "-" + month, count);
			
		}
		
		
		//供应商个数 
		StringBuffer gysHql = new StringBuffer();
		gysHql.append(" select  to_char(contract.buysigntime,'yyyy') , to_char(contract.buysigntime,'mm'), count(*)");
		gysHql.append(" from       GgZfContract contract ");
		gysHql.append(" where buysigntime between :begin and :end ");
		gysHql.append(" group by    to_char(contract.buysigntime,'yyyy') , to_char(contract.buysigntime,'mm'),contract.sellerid ");
		List<Object> gys = ggZfContractDao.find(gysHql.toString(),parameter);
		
		Map<String,Long> gysMap = new HashMap<String,Long>();
		for (int i = 0; i < gys.size(); i++) {
			Object[] object = (Object[])gys.get(i);
			if(StringUtils.isEmpty((String)object[0]))continue;
			String year = (String)object[0];
			String month = (String)object[1];
			Long count = (Long)object[2];
			gysMap.put(year + "-" + month, count);
			
		}
		
		//每个人金额汇总
		StringBuffer sumHql = new StringBuffer();
		sumHql.append(" select    to_char(contract.buysigntime,'yyyy') , to_char(contract.buysigntime,'mm'),sum(contractsum)");
		sumHql.append(" from       GgZfContract contract                  ");
		sumHql.append(" where buysigntime between :begin and :end ");
		sumHql.append(" group by   to_char(contract.buysigntime,'yyyy') , to_char(contract.buysigntime,'mm'),to_number(to_char(contract.buysigntime,'yyyymm'))");
		sumHql.append(" order by to_number(to_char(contract.buysigntime,'yyyymm')) ");
		List<Object> sum = ggZfContractDao.find(sumHql.toString(),parameter);
		
		//时间区间内总金额
		StringBuffer sumTotalHql = new StringBuffer();
		sumTotalHql.append(" select    sum(contractsum)");
		sumTotalHql.append(" from       GgZfContract contract                  ");
		sumTotalHql.append(" where buysigntime between :begin and :end ");
		List<Object> sumTotalList = ggZfContractDao.find(sumTotalHql.toString(),parameter);
		
		Double sumTotal = (Double)sumTotalList.get(0);
		
		List<ZfcgStaticsVO> resultList = new ArrayList<ZfcgStaticsVO>();
		
		Map<String,ZfcgStaticsVO>dataMap = new HashMap<String,ZfcgStaticsVO>();
		
		for (int i = 0; i < sum.size(); i++) {
			Object[] object = (Object[])sum.get(i);
			if(StringUtils.isEmpty((String)object[0]))continue;
			String year = (String)object[0];
			String month = (String)object[1];
			Double count = (Double)object[2];
			ZfcgStaticsVO zfcgStaticsVO = new ZfcgStaticsVO();
			zfcgStaticsVO.setYear(year);
			zfcgStaticsVO.setMonth(month);
			String key = year+"-"+month;
			zfcgStaticsVO.setCgrs(cgrMap.get(key).toString());
			zfcgStaticsVO.setGysrs(gysMap.get(key).toString());
			zfcgStaticsVO.setSum(count/10000);
			zfcgStaticsVO.setSumTotal(sumTotal/10000);
			BigDecimal persent = new BigDecimal(count/sumTotal * 100);
			persent.setScale(2,   BigDecimal.ROUND_HALF_UP);
			zfcgStaticsVO.setPersent(persent.doubleValue());
			zfcgStaticsVO.setYear_month(key);
			resultList.add(zfcgStaticsVO);
			dataMap.put(key, zfcgStaticsVO);
			zfcgStaticsVO.setDataMap(dataMap);
			
		}
		return resultList;
	}

}
